<template>
  <div class="administrator">
    <el-col :span="24" class="action-bar">
			<el-button type="primary" size="medium" @click="addBtnReset" style="margin: 0 20px;">新增</el-button>
    </el-col>

    <!--列表-->
		<el-table :data="adminList" highlight-current-row class="el-table-style">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60" sortable>
			</el-table-column>
      <el-table-column prop="username" label="账号" width="150" sortable>
			</el-table-column>
      <el-table-column prop="role_name" label="角色" width="150" sortable>
			</el-table-column>
			<el-table-column prop="status" label="管理员状态" width="150" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="adminSet(scope.row.id)">编辑</el-button>
          <el-button type="danger" class="danger-btn" size="mini" @click="delAdmin(scope.row.id)">删除</el-button>
        </template>
			</el-table-column>
		</el-table>
		<el-col :span="24" class="footer-bar">
			<!-- <div class="batch"><el-button size="medium ">批量删除</el-button></div> -->
			<div class="page-code">
        <el-pagination 
          background 
          layout="prev, pager, next" 
          :total="pageTotal"
					:page-size="limit"
					@current-change="getAdminList"
        >
        </el-pagination>
      </div>
    </el-col>

    <!-- 新增 -->
		<el-dialog title="新增管理员" :visible.sync="addFormVisible" width="30%" >
			<el-form :model="addForm" label-width="120px" label-position="left">
        <el-form-item label="账号">
          <el-input v-model="addForm.username" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="密码">
          <el-input v-model="addForm.pwd" type="password" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="确认密码" >
          <el-input v-model="addForm.repwd" type="repwd" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="角色">
          <el-checkbox-group  v-model="addForm.checked">
            <el-checkbox v-for="character in roleList" :label="character.id" :key="character.id">{{character.display_name}}</el-checkbox>
          </el-checkbox-group>
				</el-form-item>
				<el-form-item label="管理员状态">
          <el-select v-model="addForm.state" placeholder="请选择">
            <el-option
              v-for="item in administratorStateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addCancelBtn">取消</el-button>
				<el-button type="primary" @click.native="addSubmitBtn">提交</el-button>
			</div>
		</el-dialog>

    <!-- 编辑 -->
		<el-dialog title="编辑管理员" :visible.sync="editFormVisible" width="30%" >
			<el-form label-width="120px" label-position="left">
        <el-form-item label="账号" >
          <el-input v-model="editForm.username" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="密码">
          <el-input v-model="editForm.pwd" type="password" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="确认密码">
          <el-input v-model="editForm.repwd" type="repwd" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="角色">
          <el-checkbox-group  v-model="editForm.checked">
            <el-checkbox v-for="character in roleList" :label="character.id" :key="character.id">{{character.display_name}}</el-checkbox>
          </el-checkbox-group>
				</el-form-item>
				<el-form-item label="管理员状态" prop="characterPermission">
          <el-select v-model="editForm.state" placeholder="请选择">
            <el-option
              v-for="item in administratorStateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editCancelBtn">取消</el-button>
				<el-button type="primary" @click.native="editSubmitBtn">提交</el-button>
			</div>
		</el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return{
      addFormVisible: false,//新增界面是否显示
      editFormVisible: false,//编辑界面是否显示
      roleList:[],
      adminList:[],
      pageTotal:0,
			limit:16,
      //新增界面数据
			addForm: {
				username:'',
        pwd:'',
        repwd:'',
        character:'',
        state:'',
        checked:[]
      },
      //编辑界面数据
			editForm: {
        id:'',
        username:'',
        pwd:'',
        repwd:'',
        character:'',
        state:'',
        checked:[]
      },
      administratorStateOptions: [
        {
          value: '1',
          label: '已启用'
        }, 
        {
          value: '2',
          label: '未启用'
        }, 
      ],
    }
  },
  created(){
    this.getRoleList();
    this.getAdminList();
  },
  methods:{
    //获取管理员列表
    async getAdminList(page){
      let params = {
				limit:this.limit,
				page:page || 1,
			}
      const res = await this.$api.getAdminList(params);
      if(res.data.level=='success'){
        let list = res.data.data;
        list.forEach(item => {
          item.status = item.status==1? '启用' : '未启用';
        });       
        this.adminList = list;
        this.pageTotal = res.data.page_info.total;
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
    },
    //获取角色列表
    async getRoleList(){
      const res = await this.$api.getRoleList();
      if(res.data.level=='success'){
        this.roleList = res.data.data;
      }else{
        this.$message({
          message: res.data.message,
          type: 'error'
        });
      }
    },
    //新增取消按钮
		addCancelBtn(){
      this.addForm.username = "";
      this.addForm.pwd = "";
      this.addForm.repwd = "";
      this.addForm.character = "";
      this.addForm.state = "";
      this.addForm.checked = [];
			this.addFormVisible = false;
		},
    //编辑取消按钮
		editCancelBtn(){
      this.editForm.id = "";
      this.editForm.username = "";
      this.editForm.pwd = "";
      this.editForm.repwd = "";
      this.editForm.character = "";
      this.editForm.state = "";
      this.editForm.checked = [];
			this.editFormVisible = false;
    },
    //新增按钮重置表单
		addBtnReset(){
      this.addForm.username = "";
      this.addForm.pwd = "";
      this.addForm.repwd = "";
      this.addForm.character = "";
      this.addForm.state = "";
      this.addForm.checked = [];
			this.addFormVisible = true;
		},
    //编辑管理员
    async adminSet(id){
      const params = {
        id,
      }
      const res = await this.$api.adminSet(params);
      if(res.data.level=='success'){
        this.editForm.id = res.data.data.id;
        this.editForm.username = res.data.data.username;
        this.editForm.character = res.data.data.role_name;
        this.editForm.state = res.data.data.status;
        this.editFormVisible = true;
        let arr = res.data.data.role_id_list;
        let checkedArr = [];
        arr.forEach((item,index)=>{
          checkedArr.push(item.id)
        })
        this.editForm.checked = checkedArr;
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
    },
    //编辑提交
    async editSubmitBtn(){
      const params = {
				id:this.editForm.id,
				username:this.editForm.username,
        password:this.editForm.pwd,
        repass:this.editForm.repwd,
        status:this.editForm.state,
        role_id:this.editForm.checked,
      }
      const res = await this.$api.setAdmin(params);
      if(res.data.level=='success'){
				this.$message({
          message: '修改成功',
          type: 'success'
        });
        this.editCancelBtn();
				this.getAdminList();
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
    },
    //新增提交按钮
		async addSubmitBtn(){
			const params = {
				username:this.addForm.username,
				password:this.addForm.pwd,
				repass:this.addForm.repwd,
        status:this.addForm.state,
        role_id:this.addForm.checked,
      }
      const res = await this.$api.setAdmin(params);
      if(res.data.level=='success'){
				this.$message({
          message: '修改成功',
          type: 'success'
        });
        this.addCancelBtn();
				this.getAdminList();
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
    },
    //删除角色
		async delAdmin(id){
			const params = {
        id:id,
			}
			//删除
			const judge= this.$confirm('删除该权限?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
				}).then(async() => {
					let res = await this.$api.delAdmin(params)
					if(res.data.level=='success'){
						this.getAdminList();
						this.$message({
							type: 'success',
							message: res.data.message
						});
					}else{
						this.$message({
							type: 'error',
							message: res.data.message
						});
					}
				}).catch(() => {
			});
		}
	}
}
</script>
<style lang='scss' scoped>
  .administrator{
    .action-bar{
			background-color: #f2f2f2;
			height: 60px;
      margin: 10px 0px;
      box-sizing: border-box;
			display: flex;
			align-items: center;
			padding-left: 20px;
			.action-bar-input{
				width: 250px;
				margin-right: 14px;
			}
		}
		.el-table-style{
			width: 100%;
			border: 1px solid #f2f2f2;
		}
		.footer-bar{
			display: flex;
			justify-content: space-between;
			align-items: center;
      background: #f2f2f2;
      padding: 8px;
      margin: 10px 0px;
      box-sizing: border-box;
		}
		.dialog-footer{
			text-align: center;
		}
  }
</style>

<style>
	.el-pagination.is-background .el-pager li,
	.el-pagination.is-background .btn-prev,
	.el-pagination.is-background .btn-next
	{
		background-color: #fff;
		border: 1px solid #f1f1f1;
		margin: 0;
	}
  .member .el-dialog__body{overflow: auto;}
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>